package com.ss.android.push.window.oppo;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.bytedance.common.utility.Logger;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public final class OppoPushWindowManager {
    public WindowManager a;
    public WindowManager.LayoutParams b;
    public View c;
    public Context d;
    public Handler e;
    public String f;
    public int g;
    public int h;
    public int i;
    int j;
    public boolean k;
    public boolean l;
    public boolean m;
    boolean n;
    public int o;
    public Runnable p;
    private static OppoPushWindowManager q;
    private static a r;
    private boolean s;

    private OppoPushWindowManager(Context arg5) {

        this.e = new Handler(Looper.getMainLooper());
        this.s = false;
        this.g = 2005;
        this.j = 2;
        this.l = true;
        this.m = false;
        this.n = false;
        this.o = 0;
        this.p = new l(this);
        this.d = arg5.getApplicationContext();
        this.a = (WindowManager) arg5.getSystemService(Context.WINDOW_SERVICE);
        this.e.postDelayed(new d(this), 10000);
    }

    public static OppoPushWindowManager a(Context arg2, a arg3) {
        OppoPushWindowManager v0_1;
        Class v1 = OppoPushWindowManager.class;
        synchronized (v1){
            r = arg3;
            v0_1 = a(arg2);
        }
        return v0_1;
    }

    public static OppoPushWindowManager a(Context arg3) {
        OppoPushWindowManager v0_1;
        Class v1 = OppoPushWindowManager.class;
        synchronized (v1){
            if(r == null) {
                throw new RuntimeException("should call init(context, pushWindowDepend) in Application");
            }

            if(q == null) {
                q = new OppoPushWindowManager(arg3);
            }

            v0_1 = q;
        }
        return v0_1;
    }

    public final void a() {
        boolean v2;
        JSONObject v3;
        boolean v0 = true;
        if(!TextUtils.isEmpty(this.f)) {
            try {
                Logger.d("OppoPushWindowManager", "parsePushPopWindow: mPushPopWindowRules = " + this.f);
                v3 = new JSONObject(this.f);
                if(v3.optInt("is_show", 0) == 1) {
                    v2 = true;
                }
                else {
                    v2 = false;
                }
                this.s = v2;
                if(v3.optInt("is_cache_message", 1) == 1) {
                    v2 = true;
                } else {
                    v2 = false;
                }
                this.k = v2;
                this.i = v3.optInt("show_time_mill", 5000);
                if(v3.optInt("is_auto_dismiss", 1) != 1) {
                    v0 = false;
                }
                this.l = v0;
                this.j = v3.optInt("cache_size", 2);
                this.g = v3.optInt("type", 2005);
                this.h = v3.optInt("flag", 1160);
            }
            catch(JSONException v0_1) {
                v0_1.printStackTrace();
            }
        }
    }

    public final void a(String arg6, int arg7, String arg8) {
        if(Logger.debug()) {
            Logger.d("OppoPushWindowManager", "addCacheMessage() called with: obj = [" + arg6 + "], from = [" + arg7 + "], extra = [" + arg8 + "]");
        }

        m v1 = com.ss.android.push.window.oppo.m.a(this.d);
        if(!a(v1.a).k) {
            v1.b.a();
            return;
        }

        if(!android.support.design.a.isTextEmpty(arg6)) {
            try {
                JSONObject v0_1 = new JSONObject(arg6);
                int v2 = v0_1.optInt("id", 0);
                if(v2 <= 0) {
                    return;
                }

                if(v0_1.optInt("pass_through", 1) <= 0) {
                    return;
                }

                com.ss.android.push.window.oppo.m.a v0_2 = new com.ss.android.push.window.oppo.m.a();
                v0_2.a = v2;
                v0_2.b = 1;
                v0_2.c = arg6;
                v0_2.d = arg7;
                v0_2.e = arg8;
                v1.b.a(Integer.valueOf(v2), v0_2);
            }
            catch(Throwable v0) {
                v0.printStackTrace();
            }

            v1.a();
        }
    }

    public static void a(String arg7, long arg8, boolean arg10, JSONObject[] arg11) {
        JSONObject v6 = null;
        if(arg10) {
            if(arg11.length > 0) {
                com.ss.android.push.window.oppo.a.a("umeng", "client_apn", arg7, arg8, arg11[0]);
            }
            else {
                com.ss.android.push.window.oppo.a.a("umeng", "client_apn", arg7, arg8, v6);
            }
        }
        else if(arg11.length > 0) {
            com.ss.android.push.window.oppo.a.a("umeng", "apn", arg7, arg8, arg11[0]);
        }
        else {
            com.ss.android.push.window.oppo.a.a("umeng", "apn", arg7, arg8, v6);
        }
    }

    public final boolean b() {
        boolean v0 = !this.s || android.support.design.a.z(this.d) != 0 ? false : true;
        return v0;
    }

    public final void c() {
        try {
            this.n = false;
            this.m = false;
            this.e.removeCallbacks(this.p);
            this.a.removeViewImmediate(this.c);
        }
        catch(Exception v0) {
            if(!Logger.debug()) {
                return;
            }

            v0.printStackTrace();
        }
    }

    public final com.ss.android.push.window.oppo.m.a d() {
        Object v0_2;
        m v0 = com.ss.android.push.window.oppo.m.a(this.d);
        LinkedList v1 = new LinkedList();
        if(!a(v0.a).k) {
            v0.b.a();
        }
        else {
            Map v0_1 = v0.b.b();
            if(!v0_1.isEmpty()) {
                Iterator v2 = v0_1.entrySet().iterator();
                while(v2.hasNext()) {
                    v0_2 = v2.next();
                    if(v0_2 == null) {
                        continue;
                    }

                    ((List)v1).add(((Map.Entry)v0_2).getValue());
                }
            }
        }

        if(Logger.debug()) {
            Logger.d("OppoPushWindowManager", "getCacheMessage: list = " + v1);
        }

        if(!((List)v1).isEmpty()) {
            v0_2 = ((List)v1).get(0);
        }
        else {
           return null;
        }

        return ((com.ss.android.push.window.oppo.m.a)v0_2);
    }
}

